[...slug].md.ts 916 B

12345678910111213141516171819202122232425262728293031323334
  1. import type { APIRoute } from "astro"
  2. import { getCollection } from "astro:content"
  3. function notFoundText(locals: unknown) {
  4. if (typeof locals !== "object" || locals === null || !("t" in locals)) {
  5. return "share.not_found"
  6. }
  7. const t = (locals as { t?: unknown }).t
  8. if (typeof t !== "function") {
  9. return "share.not_found"
  10. }
  11. const text = t("share.not_found")
  12. if (typeof text === "string" && text.length > 0) {
  13. return text
  14. }
  15. return "share.not_found"
  16. }
  17. export const GET: APIRoute = async ({ params, locals }) => {
  18. const slug = params.slug || "index"
  19. const docs = await getCollection("docs")
  20. const doc = docs.find((d) => d.id === slug)
  21. const notFound = notFoundText(locals)
  22. if (!doc) {
  23. return new Response(notFound, { status: 404, statusText: notFound })
  24. }
  25. return new Response(doc.body, {
  26. headers: {
  27. "Content-Type": "text/plain; charset=utf-8",
  28. },
  29. })
  30. }